package c.x.jy.service.service.impl;

import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import c.x.jy.common.exception.RRException;
import c.x.jy.common.utils.BusinessConfigKeys;
import c.x.jy.common.utils.PageUtils;
import c.x.jy.common.utils.Query;
import c.x.jy.common.utils.SysUtils;
import c.x.jy.dao.dao.SysUserDao;
import c.x.jy.dao.entity.SysUserEntity;
import c.x.jy.service.config.BusinessConfigService;
import c.x.jy.service.entity.form.LoginForm;
import c.x.jy.service.service.SysUserService;
import c.x.jy.service.utils.SpringContextUtils;
import cn.hutool.core.lang.Assert;


@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        	QueryWrapper<SysUserEntity> query= 	 new QueryWrapper<SysUserEntity>() ;
    	if(params.get("searchKey")!=null&&params.get("searchValue")!=null&&!StringUtils.isEmpty(params.get("searchKey").toString())&&!StringUtils.isEmpty(params.get("searchValue").toString()))
    	    query.eq(  params.get("searchKey").toString(), params.get("searchValue"));
        if(params.get("businessId")!=null) 	query.eq("business_id", params.get("businessId"));
        IPage<SysUserEntity> page = this.page(
                new Query<SysUserEntity>().getPage(params),
                 query
        );

        return new PageUtils(page);
    }

 
    @Cacheable(key="'user:'+ methodName +':' +#p0",unless="#result == null")
	@Override
	public SysUserEntity selectCacheById(Long userid) {
		return baseMapper.selectById(userid);
	}

 
 
 
	@Override
	public SysUserEntity queryByMobile(String phone) {//"mobile"
		return baseMapper.selectOne(new QueryWrapper<SysUserEntity>().lambda().eq(SysUserEntity::getMobile, phone));
	}
	
	

 
	@Override
	public long login(LoginForm form) {
		SysUserEntity user = queryByMobile(form.getPhone());
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
			throw new RRException("手机号或密码错误");
		}

		return user.getUserId();
	}

 

	@Override
	public SysUserEntity queryByUserName(String username) {
		if(StringUtils.isEmpty(username))return null;
		return this.baseMapper.queryByUserName(username);
	}

	@Override
	public int restPassword(Long userId) {
		  SysUserEntity user=	this.baseMapper.selectById(userId);
		  String defultPassWord=	SpringContextUtils.getBean(BusinessConfigService.class).getBusinessConfigValue(user.getBusinessId(),BusinessConfigKeys.RESET_DEFULAT_PASSWORD);
		  String salt= SysUtils.generateCode();
		  String newPassWord= new Sha256Hash(defultPassWord, salt).toHex();
 
		  user.setPassword(newPassWord);
		  user.setSalt(salt);
		  return this.baseMapper.updateById(user);
		  
	}
	@Override
	public int SetPassword(Long userId,String oldpwd,String newPwd) {
		
		 SysUserEntity user=	this.baseMapper.selectById(userId);
		 if(!user.getPassword().equals(new Sha256Hash(oldpwd, user.getSalt()).toHex())) {
			 throw new RRException("密码错误");
		 }
		  String salt= SysUtils.generateCode(); 
		  String newPassWord= new Sha256Hash(newPwd, salt).toHex();
		  user.setPassword(newPassWord);
		  user.setSalt(salt);
		  return this.baseMapper.updateById(user);
	}

	/*
	 * @Cacheable(key="'user:'+ methodName +':' +#p0")
	 * 
	 * @Override public String selectNamebyId(Long userId) { if(userId==null)return
	 * null; SysUserEntity user= sysUserDao.selectById(userId); return
	 * user==null?null:user.getUsername(); }
	 */
 

	 

	
}
